﻿Imports Microsoft.Win32
Imports System.Collections.ObjectModel
Imports System.Runtime.InteropServices

Imports System.IO
Imports System.IO.Packaging

Public Structure ColorSettings
    Public Text As String
    Public Font As Font
    Public ForeColor As Color
    Public BackColor As Color
End Structure

Public Class frmSettings
    'Declarations
    'A collection of controls that will be placed on a new panel
    Private ctrlCollection As Collection(Of Control)
    'A Collection of all panels
    Private _Panels As Collection(Of Panel)
    'A Collection of attributes for each tab on TrayPad
    Private objColorList As New ArrayList

    Private ilbCollection As New Collection(Of DevExpress.XtraEditors.Controls.ImageListBoxItem)


    'Properties
    Public Property Panels() As Collection(Of Panel)
        Get
            If _Panels Is Nothing Then _Panels = New Collection(Of Panel)
            Return _Panels
        End Get
        Set(ByVal value As Collection(Of Panel))
            _Panels = value
        End Set
    End Property


    'Settings
    Private Sub frmSettings_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        setPanels()
        initializeSettings()

        orderButtons()
        checkButtons()

        If My.Settings.FolderPadLabelList Is Nothing Then
            My.Settings.FolderPadLabelList = New System.Collections.Specialized.StringCollection
        Else
            loadLabelFilterList()
        End If
    End Sub

    Private Sub lvwSettingTypes_ItemSelectionChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles lvwSettingTypes.ItemSelectionChanged
        'Hide every panel first
        For Each _panel As Panel In Panels
            _panel.Visible = False
        Next

        'Get the panel from the Tag property of the currently selected item and make it visible
        Dim pnl As Panel = CType(e.Item.Tag, Panel)
        pnl.Visible = True
    End Sub

    Public Sub setPanels()
        Dim pnl As Panel

        For Each tab As TabPage In tabSettingPanelHolder.TabPages
            pnl = New Panel 'Create Panel and properties
            pnl.Dock = DockStyle.Fill
            pnl.Visible = True

            'Add the panel to the 'container' panel
            pnlContent.Controls.Add(pnl)

            'Add the panel to the Panels collection to keep track of it easily
            Me.Panels.Add(pnl)

            'Create a new ListviewItem with the text and image from the current TabPage
            Dim lvi As New ListViewItem(tab.Text)
            lvi.Tag = pnl       'Important: The Tag property of the listviewitem contains the corresponding panel
            lvwSettingTypes.Items.Add(lvi)

            'For each control, add it to a new panel
            '1. Add the controls to a Collection(Of Control)
            ctrlCollection = New Collection(Of Control)
            For Each ctrl As Control In tab.Controls
                ctrlCollection.Add(ctrl)
            Next
            '2. Add the controls in the _Controls collection to the panel
            For Each ctrl As Control In ctrlCollection
                pnl.Controls.Add(ctrl)
            Next
        Next
        tabSettingPanelHolder.Visible = False 'Hide tabcontrol
    End Sub

    Public Sub initializeSettings()
        If ExistsAltap() = False Then
            chkEnableExplorer.Enabled = False
        End If

        With My.Settings
            'lblTpStyle.BackColor = .TrayPadBackColor
            'lblTpStyle.Font = .TrayPadFont
            'lblTpStyle.ForeColor = .TrayPadForeColor

            lblFpStyle.BackColor = .FolderPadBackColor
            lblFpStyle.Font = .FolderPadFont
            lblFpStyle.ForeColor = .FolderPadForeColor

            'Determines if registry key for launching one Windows startup exists.
            Dim regShell As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True)
            If regShell.GetValue("SysPad") = Application.ExecutablePath Then
                chkRun.Checked = True
            End If
            regShell.Close()

            'Other settings
            If .SysPadUpdateNotify = 1 Then
                chkNotifyUpdates.Checked = True
            End If

            If .TrayPadStart = 1 Then
                chkTrayLaunch.Checked = True
            End If

            If .uncEnabled = 1 Then
                chkUNC.Checked = True
            End If

            If .ToolstripShow = 1 Then
                chkToolstripShow.Checked = True
            End If

            If .TitleBarDisplayPad = 1 Then
                chkTitleBarDisplayPad.Checked = True
            End If
            If .TitleBarRemoveTrayPad = 1 Then
                chkTitleBarRemoveTrayPad.Checked = True
            End If

            pathBrowseDiag.RootFolder = Environment.SpecialFolder.MyComputer 'Set default Browse folder for N++

            cboTSPosition.SelectedIndex = .ToolstripLocation
            cboPadClick.SelectedIndex = .DefaultClick

            If .ExplorerDefault = 1 Then
                chkEnableExplorer.Checked = True
                txtExplorerLocation.Text = .ExplorerLocation
                txtTitlebarPrefix.Text = .AltapTitlebarPrefix
            End If

            If .ExplorerShift = 1 Then
                chkShift.Checked = True
            End If

            Select Case .AltapIconColor
                Case 0
                    rdoIconDefault.Checked = True
                Case 1
                    rdoIconGreen.Checked = True
                Case 2
                    rdoIconRed.Checked = True
                Case 3
                    rdoIconBlue.Checked = True
            End Select

            Select Case .AltapWindow
                Case 0
                    rdoWindowLeft.Checked = True
                Case 1
                    rdoWindowRight.Checked = True
            End Select
        End With

        checkExplorerControls()
    End Sub

    Public Sub saveSettings()
        With My.Settings
            .FolderPadBackColor = lblFpStyle.BackColor
            .FolderPadFont = lblFpStyle.Font
            .FolderPadForeColor = lblFpStyle.ForeColor

            If chkNotifyUpdates.Checked = True Then
                .SysPadUpdateNotify = 1
            ElseIf chkTrayLaunch.Checked = False Then
                .SysPadUpdateNotify = 0
            End If

            If chkTrayLaunch.Checked = True Then
                .TrayPadStart = 1
            ElseIf chkTrayLaunch.Checked = False Then
                .TrayPadStart = 0
            End If

            If chkUNC.Checked = True Then
                .uncEnabled = 1
            ElseIf chkUNC.Checked = False Then
                .uncEnabled = 0
            End If

            If chkToolstripShow.Checked = True Then
                .ToolstripShow = 1
                FolderPad.tsSystemLocations.Visible = True
            ElseIf chkToolstripShow.Checked = False Then
                .ToolstripShow = 0
                FolderPad.tsSystemLocations.Visible = False
            End If

            .ToolstripLocation = cboTSPosition.SelectedIndex
            Select Case .ToolstripLocation
                Case 0
                    FolderPad.tsSystemLocations.Dock = DockStyle.Left
                    FolderPad.tsSystemLocations.LayoutStyle = ToolStripLayoutStyle.VerticalStackWithOverflow
                Case 1
                    FolderPad.tsSystemLocations.Dock = DockStyle.Top
                    FolderPad.tsSystemLocations.LayoutStyle = ToolStripLayoutStyle.HorizontalStackWithOverflow
                Case 2
                    FolderPad.tsSystemLocations.Dock = DockStyle.Bottom
                    FolderPad.tsSystemLocations.LayoutStyle = ToolStripLayoutStyle.HorizontalStackWithOverflow
            End Select

            .DefaultClick = cboPadClick.SelectedIndex

            If chkEnableExplorer.Checked = True And txtExplorerLocation.Text.Length = 0 Then
                MessageBox.Show("Please define a location for Altap Salamander" & vbNewLine & "File Explorer settings not saved.", "Save Error", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            ElseIf chkEnableExplorer.Checked = True Then
                .ExplorerDefault = 1
            ElseIf chkEnableExplorer.Checked = False Then
                .ExplorerDefault = 0
                .AltapIconColor = 0
                .AltapWindow = 0
                .ExplorerShift = 0
                chkShift.Checked = False
                rdoIconDefault.Checked = True
                rdoWindowLeft.Checked = True
                txtExplorerLocation.Clear()
                txtTitlebarPrefix.Clear()
            End If

            If chkShift.Checked = True Then
                .ExplorerShift = 1
            ElseIf chkShift.Checked = False Then
                .ExplorerShift = 0
            End If

            If chkTitleBarDisplayPad.Checked = True Then
                .TitleBarDisplayPad = 1
            ElseIf chkTitleBarDisplayPad.Checked = False Then
                .TitleBarDisplayPad = 0
            End If
            If chkTitleBarRemoveTrayPad.Checked = True Then
                .TitleBarRemoveTrayPad = 1
            ElseIf chkTitleBarRemoveTrayPad.Checked = False Then
                .TitleBarRemoveTrayPad = 0
            End If

            .ExplorerLocation = txtExplorerLocation.Text
            .AltapTitlebarPrefix = txtTitlebarPrefix.Text

            If rdoIconDefault.Checked = True Then
                .AltapIconColor = 0
            ElseIf rdoIconGreen.Checked = True Then
                .AltapIconColor = 1
            ElseIf rdoIconRed.Checked = True Then
                .AltapIconColor = 2
            ElseIf rdoIconBlue.Checked = True Then
                .AltapIconColor = 3
            End If

            My.Settings.SystemLocationsOrder = New ArrayList
            For Each i As DevExpress.XtraEditors.Controls.ImageListBoxItem In lstDisplay.Items
                My.Settings.SystemLocationsOrder.Add(i.Value)
            Next

            .Save()
        End With

        FolderPad.reorderButtons()
        If TrayPadHasOpened Then
            TrayPad.setTitleBar(TrayPad.tabControl.SelectedTab.Text)
        End If
    End Sub

    Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click
        saveSettings()
        Me.Dispose()
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Dispose()
    End Sub

    Private Sub btnApply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnApply.Click
        saveSettings()
    End Sub


    'General
    Private Sub chkRun_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkRun.CheckedChanged
        Dim regShell As RegistryKey = Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run", True)
        If chkRun.Checked = True Then
            regShell.SetValue("SysPad", Application.ExecutablePath)
        ElseIf chkRun.Checked = False Then
            regShell.DeleteValue("SysPad", True)
        End If
        regShell.Close()
    End Sub

    Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click
        Dim folder As String = String.Empty
        Dim diag As New FolderBrowserDialog

        diag.RootFolder = Environment.SpecialFolder.MyComputer
        diag.Description = "Folder Location to save syspad.dat"

        If diag.ShowDialog() = DialogResult.OK Then
            ExportData(diag.SelectedPath)
        End If
    End Sub

    Private Sub ExportData(ByVal filePath As String)

    End Sub

    Private Sub btnImport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImport.Click
        Dim folder As String = String.Empty
        Dim diag As New FolderBrowserDialog

        diag.RootFolder = Environment.SpecialFolder.MyComputer
        diag.Description = "Folder Location of syspad.dat"

        If diag.ShowDialog() = DialogResult.OK Then

        End If
    End Sub

    Private Sub ImportData(ByVal filePath As String, ByVal zipFile As String)

    End Sub

    Private Sub chkTitleBarDisplayPad_CheckStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkTitleBarDisplayPad.CheckStateChanged
        If chkTitleBarDisplayPad.Checked = True Then
            chkTitleBarRemoveTrayPad.Enabled = True
        Else
            chkTitleBarRemoveTrayPad.Enabled = False
        End If
    End Sub


    'FolderPad Fonts & Colors
    Private Sub tsbtnFpFont_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbtnFpFont.Click
        FontDialog.Font = My.Settings.FolderPadFont
        If FontDialog.ShowDialog = DialogResult.OK Then
            lblFpStyle.Font = FontDialog.Font
        End If
    End Sub

    Private Sub tsbtnFpForeColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbtnFpForeColor.Click
        ColorDialog.Color = My.Settings.FolderPadForeColor
        If ColorDialog.ShowDialog = DialogResult.OK Then
            lblFpStyle.ForeColor = ColorDialog.Color
        End If
    End Sub

    Private Sub tsbtnFpBackColor_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbtnFpBackColor.Click
        ColorDialog.Color = My.Settings.FolderPadBackColor
        If ColorDialog.ShowDialog = DialogResult.OK Then
            lblFpStyle.BackColor = ColorDialog.Color
        End If
    End Sub


    'System Toolstrip
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        lstDisplay.Items.Add(lstPool.SelectedItem)
        lstPool.Items.RemoveAt(lstPool.SelectedIndex)
        checkButtons()
    End Sub

    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        lstPool.Items.Add(lstDisplay.SelectedItem)
        lstDisplay.Items.RemoveAt(lstDisplay.SelectedIndex)
        checkButtons()
    End Sub

    Private Sub btnUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUp.Click
        Dim selectedIndex As Integer = lstDisplay.SelectedIndex

        lstDisplay.Items.Insert(selectedIndex - 1, lstDisplay.SelectedItem)
        lstDisplay.Items.RemoveAt(selectedIndex + 1)
        lstDisplay.SelectedIndex = selectedIndex - 1
        checkButtons()
    End Sub

    Private Sub btnDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDown.Click
        Dim selectedIndex As Integer = lstDisplay.SelectedIndex

        lstDisplay.Items.Insert(selectedIndex + 2, lstDisplay.SelectedItem)
        lstDisplay.Items.RemoveAt(selectedIndex)
        lstDisplay.SelectedIndex = selectedIndex + 1
        checkButtons()
    End Sub

    Public Sub orderButtons()
        If My.Settings.SystemLocationsOrder IsNot Nothing AndAlso My.Settings.SystemLocationsOrder.Count > 0 Then
            For Each ilb As DevExpress.XtraEditors.Controls.ImageListBoxItem In lstPool.Items
                ilbCollection.Add(CType(ilb, DevExpress.XtraEditors.Controls.ImageListBoxItem))
            Next
            lstPool.Items.Clear()

            With My.Settings
                For i As Integer = 0 To .SystemLocationsOrder.Count - 1 Step 1
                    Dim lstItemName As String = .SystemLocationsOrder.Item(i)
                    For Each o As DevExpress.XtraEditors.Controls.ImageListBoxItem In ilbCollection
                        If o.Value = lstItemName Then
                            lstDisplay.Items.Add(o)
                        End If
                    Next
                Next

                For Each o As DevExpress.XtraEditors.Controls.ImageListBoxItem In ilbCollection
                    If Not .SystemLocationsOrder.Contains(o.Value) Then
                        lstPool.Items.Add(o)
                    End If
                Next
            End With
        End If
    End Sub

    Private Sub lstDisplay_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstDisplay.SelectedIndexChanged
        checkButtons()
    End Sub

    Public Sub checkButtons()
        If lstPool.Items.Count = 0 Then
            btnAdd.Enabled = False
        Else
            btnAdd.Enabled = True
        End If

        If lstDisplay.Items.Count = 0 Then
            btnRemove.Enabled = False
        Else
            btnRemove.Enabled = True
        End If

        If lstDisplay.SelectedIndex = 0 Or lstDisplay.SelectedIndex = -1 Or lstDisplay.Items.Count = 0 Then
            btnUp.Enabled = False
        Else
            btnUp.Enabled = True
        End If

        If lstDisplay.SelectedIndex = lstDisplay.Items.Count - 1 Or lstDisplay.SelectedIndex = -1 Or lstDisplay.Items.Count = 0 Then
            btnDown.Enabled = False
        Else
            btnDown.Enabled = True
        End If
    End Sub


    'File Explorer
    Private Sub chkEnableExplorer_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkEnableExplorer.CheckedChanged
        checkExplorerControls()
    End Sub

    Private Sub btnExplorerBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExplorerBrowse.Click
        pathBrowseDiag.RootFolder = Environment.SpecialFolder.MyComputer
        pathBrowseDiag.Description = "Locate: salamand.exe (Altap Salamander)"
        If pathBrowseDiag.ShowDialog() = Windows.Forms.DialogResult.OK Then
            If System.IO.File.Exists(pathBrowseDiag.SelectedPath & "\salamand.exe") = True Then
                txtExplorerLocation.Text = pathBrowseDiag.SelectedPath
            Else
                MessageBox.Show("Altap Salamander executable is not in this folder." & vbNewLine & "Executable path not set.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop)
            End If
        End If
    End Sub

    Private Sub checkExplorerControls()
        If chkEnableExplorer.Checked = True Then
            btnExplorerBrowse.Enabled = True
            chkShift.Enabled = True
            txtTitlebarPrefix.Enabled = True
            rdoIconDefault.Enabled = True
            rdoIconGreen.Enabled = True
            rdoIconRed.Enabled = True
            rdoIconBlue.Enabled = True
            rdoWindowLeft.Enabled = True
            rdoWindowRight.Enabled = True

            lblExplorerLocation.Enabled = True
            lblShift.Enabled = True
            grpParameters.Enabled = True
            grpIcon.Enabled = True
            grpWindow.Enabled = True
        ElseIf chkEnableExplorer.Checked = False Then
            btnExplorerBrowse.Enabled = False
            chkShift.Enabled = False
            txtTitlebarPrefix.Enabled = False
            rdoIconDefault.Enabled = False
            rdoIconGreen.Enabled = False
            rdoIconRed.Enabled = False
            rdoIconBlue.Enabled = False
            rdoWindowLeft.Enabled = False
            rdoWindowRight.Enabled = False

            lblExplorerLocation.Enabled = False
            lblShift.Enabled = False
            grpParameters.Enabled = False
            grpIcon.Enabled = False
            grpWindow.Enabled = False
        End If
    End Sub

    Private Sub lblGetAltapSalamander_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lblGetAltapSalamander.LinkClicked
        Process.Start("http://www.altap.cz/download.html#sal25")
    End Sub


    'Filter List
    Private Sub loadLabelFilterList()
        lstTags.Items.Clear()
        For Each filter As String In My.Settings.FolderPadLabelList
            lstTags.Items.Add(filter)
        Next
    End Sub

    Private Sub tsbtnTagAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbtnTagAdd.Click
        Dim result As String = InputBox("New Filter Label:", "Label List")

        If result <> "" Then
            If Not My.Settings.FolderPadLabelList.Contains(result) Then
                My.Settings.FolderPadLabelList.Add(result)
                My.Settings.Save()
                loadLabelFilterList()
                If FolderPad.IsAvailable Then
                    FolderPad.loadTagList()
                End If
            Else
                MessageBox.Show(String.Format("The filter ({0}) already exists.", result))
            End If
        End If
    End Sub

    Private Sub tsbtnTagEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbtnTagEdit.Click
        If lstTags.SelectedIndex <> -1 Then
            Dim result As String = InputBox("Edit Filter Label:", "Label List", lstTags.SelectedItem)

            If result <> "" Then
                If Not My.Settings.FolderPadLabelList.Contains(result) Then
                    My.Settings.FolderPadLabelList.Item(lstTags.SelectedIndex) = result
                    My.Settings.Save()
                    loadLabelFilterList()
                    If FolderPad.IsAvailable Then
                        FolderPad.loadTagList()
                    End If
                Else
                    MessageBox.Show(String.Format("The filter ({0}) already exists.", result))
                End If
            End If
        End If
    End Sub

    Private Sub tsbtnTagDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tsbtnTagDelete.Click
        If lstTags.SelectedIndex <> -1 Then
            If MessageBox.Show(String.Format("Are you sure you want to delete this tag: {0}?", lstTags.SelectedItem), "Delete Tag", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                If My.Settings.FolderPadLabelList.Contains(lstTags.SelectedItem) Then
                    My.Settings.FolderPadLabelList.RemoveAt(lstTags.SelectedIndex)
                    My.Settings.Save()
                    lstTags.Items.Clear()
                    For Each filter As String In My.Settings.FolderPadLabelList
                        lstTags.Items.Add(filter)
                    Next
                    If FolderPad.IsAvailable Then
                        FolderPad.loadTagList()
                    End If
                Else
                    MessageBox.Show(String.Format("Could not delete this tag: {0}?", "Error", lstTags.SelectedItem))
                End If
            End If
        End If
    End Sub

End Class